﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word
{
    //TODO: Implementare WidthType per tutti i lati
    public class TableCellMargin : OpenXMLEntity
    {
        [DefaultValue(WidthType.Nil)]
        public WidthType Type
        {
            get;
            set;
        }

        public decimal? Bottom
        {
            get;
            set;
        }

        public decimal? Top
        {
            get;
            set;
        }

        public decimal? Left
        {
            get;
            set;
        }

        public decimal? Right
        {
            get;
            set;
        }

        protected internal void ParseInternal(XElement tblMargins)
        {
            var top = tblMargins.Element(NS.WM + "top");
            if (top != null)
            {
                this.Top = decimal.Parse(top.Attribute(NS.WM + "w").Value);
                this.Type = top.Attribute(NS.WM + "type").Value.GetEnum<WidthType>();
            }

            var left = tblMargins.Element(NS.WM + "left");

            if (left != null)
            {
                this.Left = decimal.Parse(left.Attribute(NS.WM + "w").Value);
                this.Type = left.Attribute(NS.WM + "type").Value.GetEnum<WidthType>();
            }
            var bottom = tblMargins.Element(NS.WM + "bottom");

            if (bottom != null)
            {
                this.Bottom = decimal.Parse(bottom.Attribute(NS.WM + "w").Value);
                this.Type = bottom.Attribute(NS.WM + "type").Value.GetEnum<WidthType>();
            }
            var right = tblMargins.Element(NS.WM + "right");

            if (right != null)
            {
                this.Right = decimal.Parse(right.Attribute(NS.WM + "w").Value);
                this.Type = right.Attribute(NS.WM + "type").Value.GetEnum<WidthType>();
            }
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement xMargins = new XElement(NS.WM + "tcMar");

            if (this.Top != null)
            {
                xMargins.Add(new XElement(NS.WM + "top",
                        new XAttribute(NS.WM + "type", this.Type.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", this.Top.Value)));
            }
            else
            {
                xMargins.Add(new XElement(NS.WM + "top",
                                        new XAttribute(NS.WM + "type",
                WidthType.Nil.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", "0")));
            }


            if (this.Left != null)
            {
                xMargins.Add(new XElement(NS.WM + "left",
                        new XAttribute(NS.WM + "type", this.Type.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", this.Left.Value)));
            }
            else
            {
                xMargins.Add(new XElement(NS.WM + "left",
                                        new XAttribute(NS.WM + "type",
                WidthType.Nil.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", "0")));
            }


            if (this.Bottom != null)
            {
                xMargins.Add(new XElement(NS.WM + "bottom",
                        new XAttribute(NS.WM + "type", this.Type.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", this.Bottom.Value)));
            }
            else
            {
                xMargins.Add(new XElement(NS.WM + "bottom",
                                        new XAttribute(NS.WM + "type",
                WidthType.Nil.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", "0")));
            }


            if (this.Right != null)
            {
                xMargins.Add(new XElement(NS.WM + "right",
                        new XAttribute(NS.WM + "type", this.Type.ToString().ToLower()),
                        new XAttribute(NS.WM + "w", this.Right.Value)));
            }
            else
            {
                xMargins.Add(new XElement(NS.WM + "right",
                                        new XAttribute(NS.WM + "type",
                WidthType.Nil),
                        new XAttribute(NS.WM + "w", "0")));
            }

            return xMargins;
        }
    }
}
